<?php
 session_start();
 include("include/session.php");
 include("include/common.php");
 include("include/db.php");
 
  $sqlCategory	=	mysql_query("SELECT * FROM software_courses WHERE batch_code = '".$_REQUEST['courses']."'");
  $resCategory	=   mysql_fetch_array($sqlCategory); 
  $category		=   $resCategory["category"];
  $trainer_id	=   $resCategory["trainer_id"];

  $sqlUsersCourses = mysql_query("SELECT * FROM software_users_courses_assign WHERE courses_id = '".$_REQUEST['courses']."' AND user_course_type = 'Student'");
  while($resUsersCourses = mysql_fetch_array($sqlUsersCourses) ){
		$userStdCourse[] = $resUsersCourses["user_id"];   
  }
  

  if(isset($_REQUEST['submit']))
  {


	$deleteStudent = mysql_query("DELETE FROM software_users_courses_assign WHERE courses_id = '".$_REQUEST['courses']."'"); 
	$trainer 	   = mysql_query("INSERT INTO software_users_courses_assign 
									SET
										time_id = unix_timestamp(),
										courses_id 		 = '".$_REQUEST['courses']."',
										course 			 = '$category',
										user_id 		 = '".$_REQUEST['trainer']."',
										user_course_type = 'Trainer'
										");
										
						
	$updateTrainers = mysql_query("UPDATE software_courses SET trainer_id = '".$_REQUEST['trainer']."' where batch_code = '".$_REQUEST['courses']."'");	
	mysql_query("UPDATE software_user_courses SET  course_id = '".$_REQUEST['courses']."',assign_course = 'Y' where user_id = '".$_REQUEST['trainer']."' AND user_course = '$category'");
	
									
		
	$students = $_REQUEST['chkAssignStd'];

	$sqlSchedule = mysql_query("SELECT * FROM software_schedule_class WHERE course = '".$_REQUEST['courses']."'");
	while($resSchedule = mysql_fetch_array($sqlSchedule)){
		$courseClsp[$resSchedule["course_class"]]["start_date"] = $resSchedule["start_time"] ;
		$courseClsp[$resSchedule["course_class"]]["end_date"]   = $resSchedule["end_time"];
	}

	if(!empty($students))
	{
		foreach($students as $std)
		{
				$sqlUserCourse = mysql_query("INSERT INTO software_users_courses_assign 
													SET time_id = unix_timestamp(),
															courses_id 		 = '".$_REQUEST['courses']."',
															course 			 = '$category',
															user_id 		 = '$std',
															user_course_type = 'Student'
											 ");
				 mysql_query("UPDATE software_user_courses SET  assign_course = 'Y' where user_id = '$std' AND user_course = '$category'");	
				 
				if(!empty($courseClsp))
				{
					foreach($courseClsp as $k => $cls){
							
							$sqlUserCourse = mysql_query("INSERT INTO software_user_cls_schedule 
															SET time_id = unix_timestamp(),
																	cls_start 		 = '".$cls['start_date']."',
																	cls_end 		 = '".$cls['end_date']."',
																	course			 = '".$_REQUEST['courses']."',
																	category		 = '$category',
																	user_id 		 = '$std',
																	class			 = '$k'
											 			");
						
					}
				}
		}
    } 
	
	header("location:associateCourse.php?msg=1&courses=".$_REQUEST['courses']);
  }
  
  
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?=TITLE_WEBSITE?></title>
<link href="css/c_energy.css" rel="stylesheet" type="text/css" />
<link href="css/logo.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" media="screen" type="text/css" href="css/layout.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/datepicker.js"></script>
<script type="text/javascript" src="js/eye.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript">
</script>
<style type="text/css">
<!--
.style4 {
	font-size: 22px;
	font-weight: bold;
}
.style6 {color: #999999}
-->
</style>
</head>
<script>
function validation(){

	var message = '';
	if(document.getElementById("trainer").value == ""){
		message += 'Please enter course category\n';
	}
	if(message != ""){
		alert(message);
		return false;
	}
}
</script>
<body>

    <table width="990" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="3" align="left" valign="top" ><? require("header.php");?></td>
</tr>
  <tr>
    <td align="center" >
	<table width="99%" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3" align="center" valign="top">&nbsp;</td>
      </tr>
       <tr bgcolor="#FAFAFA">
        <td colspan="0" align="left" valign="top" >
		   <table width="96%" cellspacing="0" cellpadding="0">
		    <tr>
		      <td width="2%" align="left" valign="top" style="padding-left:15px" ><? require("left.php");?></td>
		      <td width="94%"  valign="top" style="padding-left:15px" >
			  <form name="signform" action="" method="post" enctype="multipart/form-data" id="signform" onsubmit="return validation();" >
			    <table id="signInTbl" width="719" cellspacing="0" cellpadding="0" style="border:solid #cccccc 1px">
                  <tr>
                    <td colspan="2" align="left" bgcolor="#373737" class="loc_search_found_term"  style="border-bottom:solid #cccccc 1px">Course Assignment</td>
                  </tr>
                 <?php
                  	if($_REQUEST['msg'] == 1)
					{
				  ?>
                  <tr style="background: #D5FFD5 " height="25">
                 	<td colspan="2"><div align="left" class="successMsg" id="err_site" style="display:block">
                       &nbsp; <strong><?=($_REQUEST['msg'] == 1 ? 'Course Assignment udpated successfully' : 'Sorry,record does not inserted.')?></strong>
                    </div></td>
                  </tr>
                  <?php
                  	}elseif($_REQUEST['msg'] == 2){
				  ?>
                  <tr style="background: #FFE9E9 " height="25">
                 	<td colspan="2"><div align="left" class="ErrorMsg" id="err_site" style="display:block">
                       &nbsp; <strong><?=($_REQUEST['msg'] == 1 ? 'Course Assignment udpated successfully' : 'Sorry,record does not inserted.')?></strong>
                    </div></td>
                  </tr>	
				  <?php		
					}
				  ?>
                  <tr height="30">
                    <td width="249" align="left" style="padding-left:15px"> Course</td>
                    <td width="466" align="left">
                        <select id="courses" name="courses" onchange="window.location.href='associateCourse.php?courses='+this.value" >
                        	<option value="" >Select</option>
                        	<?php
                       		 $sqlCourses = mysql_query("SELECT name,batch_code FROM software_courses");
                        	 while($resCourses = mysql_fetch_array($sqlCourses)){
                        	?>
                        	<option value="<?=$resCourses["batch_code"]?>" <?=$_REQUEST['courses'] == $resCourses ["batch_code"] ? 'selected=selected' : '' ?> ><?=$resCourses["batch_code"]?></option>
                        	<?php		
                        	}
                        	?>
                        </select>
                    </td>
                </tr> 
                 <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Trainer </td>
                    <td width="466" align="left">
                    	<select id="trainer" name="trainer">
                        	<option value="" >Select</option>
                        	<?php
                       		 $sqlCourses = mysql_query("SELECT user_id,email,first_name,mobile FROM software_user as SU 
																INNER JOIN software_user_courses as SUC
																	ON SU.id = SUC.user_id
																WHERE SUC.user_type = 'Trainer'  AND SUC.user_course = '".$category."'  
																	 AND SU.active = 'Y'");
                        	 while($resCourses = mysql_fetch_array($sqlCourses)){
                        	?>
                        	<option value="<?=$resCourses["user_id"]?>" <?=($trainer_id == $resCourses["user_id"] ? 'selected=selected' : '')?> ><?=$resCourses["first_name"]?></option>
                        	<?php		
                        	}
                        	?>
                        </select>
                    
                    </td>
                </tr>
                <tr>
                	<td colspan="2" align="center">
                    	<table width="600" style="border:1px solid #000" >
                        	<tr style=" background:#000;color:#FFF; font-weight:bold">
                            	<td style="color:#FFF">Student Name</td>
                                <td style="color:#FFF">Student Email</td>
                                <td style="color:#FFF">Category</td>
                                <td><input type="checkbox" id="" name=""  /></td>
                                <td></td>
                            </tr>
                            <?php
							$sqlStudents = mysql_query("SELECT user_id,email,first_name,mobile FROM software_user as SU 
																INNER JOIN software_user_courses as SUC
																	ON SU.id = SUC.user_id
																WHERE SUC.user_type = 'Student'  AND SUC.user_course = '".$category."'  
																	 AND SU.active = 'Y'");
							while($resStudents = mysql_fetch_array($sqlStudents))
							{									
							?>
                            <tr>
                            	<td><?=$resStudents["first_name"]?></td>
                                <td><?=$resStudents["email"]?></td>
                                <td><?=$resStudents["user_course"]?></td>
                                <td><input type="checkbox" id="chkAssignStd[]" name="chkAssignStd[]" value="<?=$resStudents["user_id"]?>"
                                		<?php if(@in_array($resStudents["user_id"],$userStdCourse)){ echo 'checked=checked'; }else{ echo "";}?>
                                	/></td>
                                <td></td>
                            </tr>
                            <?php
                            }
							?>
                        </table>
                    </td>
                
                </tr>
                
                   <tr  height="30">
                    <td width="249"  style="padding-left:15px" colspan="2" align="center"><input class="cls_button" type="submit" id="submit" name="submit" value="Submit"  /></td>
                  </tr>
                </table>
			    <ul class="site_nav_menu_buttons">
			      <li class="no_menu_li"><span><a href="courses.php">&nbsp;Back to Courses </a></span></li>
			      </ul>
			    <p>&nbsp;</p>
			  </form>			  </td>
            </tr>
	        </table></td>
        </tr>
      <tr bgcolor="#FAFAFA">
        <td width="800" align="center" valign="top">&nbsp;</td>
        <td width="100" colspan="2">&nbsp;</td>
      </tr>
	    <tr bgcolor="#FAFAFA">
        <td width="800" align="center" valign="top">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

